#!/usr/bin/python3
from flask import request
from flask import make_response
from flask import url_for
from flask import Flask
from flask import render_template
from flask_sqlalchemy import SQLAlchemy
from datetime import datetime
#import pymysql
#import MySQLdb
app = Flask(__name__)
#连接数据库
app.config['SQLALCHEMY_DATABASE_URI'] = \
 "mysql+pymysql://xlb:123123@192.168.0.120/test"
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = True
db = SQLAlchemy(app)
#定义的类
class Users(db.Model):
    __tablename__ = 'users'
 
    id = db.Column(db.Integer, primary_key=True)
    email = db.Column(db.String)
    username = db.Column(db.String)
    passwd = db.Column(db.String)
    data = db.Column(db.DateTime)
#插入数据
user = Users(id=1,email='ttlsa@ttlsa.com',username='ttlsa',passwd='ttlsa',data=datetime.now())
db.session.add(user)
#db.session.add(user_detail)
db.session.commit()

#修改数据
#users = Users.query.filter_by(id=5).first()
#users.username='cali1'
#db.session.add(users)
#db.session.commit()

@app.route('/')
def v_index():
    rsp = make_response('go <a href="%s">page2</a>'%url_for('v_page2'))
    rsp.set_cookie('user','zhaoliying')
    return rsp
@app.route('/page2')
def v_page2():
    user = request.cookies['user']
    return 'you are {0}'.format(user)
@app.route('/login')
def hi():
    #return 'your name is {}'.format(request.form['username'])
    return render_template('login.html')
if __name__ == '__main__':
    app.run(host='0.0.0.0',port=80,debug=True,threaded=True)


